package com.hktf.uilib.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flask.colorpicker.ColorPickerView;
import com.flask.colorpicker.OnColorSelectedListener;
import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
import com.hktf.corelib.constant.CommonConstant;
import com.hktf.corelib.constant.KEYS;
import com.hktf.corelib.utils.MMKVUtils;
import com.hktf.corelib.utils.UIUtils;
import com.hktf.uilib.adapter.ColorItemAdapter;
import com.hktf.uilib.adapter.FontItemAdapter;
import com.hktf.uilib.app.BaseActivity;
import com.hktf.uilib.bean.ColorItem;
import com.hktf.uilib.bean.FontItem;
import com.hktf.uilib.bean.KeyValueItem;
import com.hktf.uilib.R;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PickFontAndColorPopWindow extends PopupWindow {
    private RecyclerView recyclerView_font;

    private FontItemAdapter fontItemAdapter;

    private int initColor = -1;

    public int getInitColor() {
        return initColor;
    }

    public void setInitColor(int initColor) {
        this.initColor = initColor;
    }

    public PickFontAndColorPopWindow(final BaseActivity context, Integer initCorlor, MyListener listener) {
        this.myListener = listener;
        View rootView = View.inflate(context, R.layout.pick_font_color_pop, null);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        recyclerView_font = rootView.findViewById(R.id.recyclerView_font);
        recyclerView_font.setLayoutManager(new GridLayoutManager(context, 3));
//

        final TextView tvTitle = rootView.findViewById(R.id.tv_title);
        rootView.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
//        rootView.findViewById(R.id.tv_title).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //                pickColor(context);
//                pickColorDialog(context, tvTitle);
//            }
//        });

//        int[] fontResIds = new int[]{
//                R.font.type1, R.font.type2, R.font.type3,
//                R.font.type4, R.font.type5, R.font.type6,
//                R.font.type7, R.font.type8, R.font.type9
//        };

        index=-1;
        int FONT = MMKVUtils.getInteger(KEYS.FONT, -1);
        List<FontItem> fontItemList = Arrays.stream(CommonConstant.FONT_RES_IDS)
                .mapToObj(fontId -> {
                    index++;
                    FontItem fontItem = new FontItem(false, fontId);
                    if(index==FONT){
                        fontItem.setCheck(true);
                        preIndex=index;
                    }
                    return fontItem;
                })
                .collect(Collectors.toList());

        fontItemAdapter = new FontItemAdapter(fontItemList, context);
        recyclerView_font.setAdapter(fontItemAdapter);
        setContentView(rootView);
        setOutsideTouchable(true);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        fontItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {


            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (preIndex == position) {
                    return;
                }
                if (preIndex != -1) {
                    fontItemAdapter.getItem(preIndex).setCheck(false);
                }
                FontItem item = fontItemAdapter.getItem(position);
                item.setCheck(true);
                fontItemAdapter.notifyDataSetChanged();
                preIndex = position;

                if (myListener != null) {
                    myListener.onPickFont(item.getFontResId(),position);
                }

            }
        });

        initColor(rootView,context);

    }
    private int preIndex = -1;
    private int colorPreIndex = -1;
    private int index;
    private void initColor(View rootview,BaseActivity context){
        int[] colorIds = new int[]{
                R.color.color1, R.color.color2, R.color.color3,
                R.color.color4, R.color.color5, R.color.color6,
                R.color.color7, R.color.color8, R.color.color9,
                R.color.color10, R.color.color11

        };
        index=-1;

        int FontColor = MMKVUtils.getInteger(KEYS.FONT_COLOR, -1);
        List<ColorItem> colorItemList = Arrays.stream(colorIds)
                .mapToObj(i -> {
                    index++;
                    ColorItem item = new ColorItem(false, UIUtils.getColor(i));
                    if(item.getColor()==FontColor){
                        item.setCheck(true);
                        colorPreIndex=index;
                    }
                    return item;
                })
                .collect(Collectors.toList());

        ColorItem item=new ColorItem(false,-1);
        item.setShowIcon(true);
        colorItemList.add(item);

        RecyclerView recyclerView=rootview.findViewById(R.id.recyclerView_color);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        ColorItemAdapter colorItemAdapter=new ColorItemAdapter(colorItemList);
        colorItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {


            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ColorItemAdapter colorItemAdapter= (ColorItemAdapter) adapter;
                ColorItem item = colorItemAdapter.getItem(position);
                if (item.isShowIcon()) {
                    //自定义颜色
                    pickColorDialog(context, rootview);
                    return;
                }
                if (colorPreIndex == position) {
                    return;
                }
                if (colorPreIndex != -1) {
                    colorItemAdapter.getItem(colorPreIndex).setCheck(false);
                }
                item.setCheck(true);
                colorItemAdapter.notifyDataSetChanged();
                colorPreIndex = position;

                if (myListener != null) {
                    myListener.onPickColor(item.getColor());
                }

            }
        });
        recyclerView.setAdapter(colorItemAdapter);

    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (myListener != null) {
            myListener.onDismiss();
        }
    }

    private void pickColorDialog(BaseActivity context, final View tv) {
        //        DialogFragment dialogFragment = new AdaptedDialogFragment();
        //        dialogFragment.show(context.getSupportFragmentManager(), "adapted_dialog");

        PickColorPopWindow pickColorPopWindow = new PickColorPopWindow(context, initColor, new PickColorPopWindow.MyListener() {

            @Override
            public void onPickColor(int color) {
                if (myListener != null) {
                    myListener.onPickColor(color);
                }

            }
        });
        pickColorPopWindow.showAsDropDown(tv);

    }

    private void pickColor(Context context) {
        ColorPickerDialogBuilder
                .with(context)
                .setTitle("Choose color")
                .initialColor(R.color.owner_blue)
                .wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
                .density(12)
                .setOnColorSelectedListener(new OnColorSelectedListener() {
                    @Override
                    public void onColorSelected(int selectedColor) {
                        System.out.println("onColorSelected: 0x" + Integer.toHexString(selectedColor));
                    }
                })
                .setPositiveButton("ok", new ColorPickerClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
                        if (myListener != null) {
                            myListener.onPickColor(selectedColor);
                        }
                    }
                })
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .build()
                .show();
    }

    private MyListener myListener;

    public MyListener getMyListener() {
        return myListener;
    }

    public void setMyListener(MyListener myListener) {
        this.myListener = myListener;
    }

    public static interface MyListener {
        void onPickFont(int font,int index);

        void onPickColor(int color);
        void onDismiss();
    }

    private OnCheckChangeListener onCheckChangeListener;

    public void setOnCheckChangeListener(OnCheckChangeListener onCheckChangeListener) {
        this.onCheckChangeListener = onCheckChangeListener;
    }

    public static interface OnCheckChangeListener {
        void onCheckChange(KeyValueItem keyValueItem, int position);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
    }
}
